package ChatRoom;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ClientMain {

	TextArea ta_all = null;
	Button bt_send = null;
	DataInputStream dis = null;
	DataOutputStream dos = null;
	boolean ta_keyFlag = false;
	List l_user = null;
	Label lb_input = null;

	void launchFrame(Socket s_client, String userID) {

		final Socket s_multiuser = s_client;	//why???
		
		Frame f_client = new Frame(userID);
		ta_all = new TextArea();
		ta_all.setBounds(10, 24, 485, 300);
		ta_all.setEditable(false);

		final TextArea ta_input = new TextArea();
		ta_input.setBounds(10, 324, 485, 130);

		ta_input.addKeyListener(new KeyAdapter() {

			public void keyPressed(KeyEvent e) {
				switch (e.getKeyCode()) {
				case KeyEvent.VK_ENTER:
					String s_input = ta_input.getText().trim();
					if (ta_keyFlag && !s_input.equals("")) {
						send(Code.msg, s_input);
						ta_input.setText("");
					}
					
					break;
				case KeyEvent.VK_CONTROL:
					ta_keyFlag = true;
					break;
				}
			}

			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
					ta_keyFlag = false;
				}
			}
		});

		lb_input = new Label();
		lb_input.setBounds(23, 460, 200, 30);

		bt_send = new Button("Send");
		bt_send.setBounds(400, 460, 80, 30);
		bt_send.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s1 = ta_input.getText().trim();
				if(!s1.equals("")){
					send(Code.msg, s1);
				}
				ta_input.setText("");
			}
		});
		
		l_user = new List();
		l_user.setBounds(495, 27, 203, 473);
		l_user.setMultipleMode(true);
		
		l_user.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				String[] items = l_user.getSelectedItems();
				new MultiUsrChat().launchFrame(s_multiuser, items);
				
				for(int i=0; i < items.length; i++){
					pt(items[i]);
				}
			}
		});

		f_client.setBounds(300, 150, 700, 500);

		f_client.add(ta_all);
		f_client.add(ta_input);
		f_client.add(lb_input);
		f_client.add(bt_send);
		f_client.add(l_user);
		f_client.add(new Label());

		f_client.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				send(Code.userExit, "");
				System.exit(0);
			}
		});

		f_client.setResizable(false);
		f_client.setVisible(true);

		try {
			dis = new DataInputStream(s_client.getInputStream());
			dos = new DataOutputStream(s_client.getOutputStream());
		} catch (Exception e1) {
			lb_input.setText("can not connect to Server!");
		}

		new Thread(new ChatRecordWatcher()).start();
	}

	void send(Byte b, String s) {
		try {
			dos.writeUTF(b + s);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	void p(String s) {
		ta_all.setText(ta_all.getText() + '\n' + s);
	}

	class ChatRecordWatcher implements Runnable {

		public void run() {
			String server_msg = null;
			byte code;
			String str = null;
			while (true) {
				try {
					server_msg = dis.readUTF();
					
					if(server_msg.length() < 1){
						lb_input.setText("ERROR! msg from server is null");
						break;
					}
					
					code = Byte.parseByte(server_msg.substring(0, 1));
					
					if(server_msg.length() > 1){
						str = server_msg.substring(1);
					}
					
					switch(code){
						case Code.msg:
							p(str);
							break;
						case Code.newUser:
							l_user.add(str);
							break;
						case Code.userExit:
							l_user.remove(str);
							p("[" + str + "] : logout.");
							break;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void pt(Object o) {
		System.out.println(o);
	}
}
